package com.ezregex;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;

import static com.ezregex.RegexBuilder.*;

class Steps implements RepetitionStep, CardinalityStep, FollowedBy, BuildStep {

    Steps() {
        this.regexSegments = new ArrayList<RegexSegment>();
    }

    List<RegexSegment> regexSegments;

    @Override
    public String build() {
        String regex = "";
        for (RegexSegment regexSegment : regexSegments) {
            switch (regexSegment.getCardinality()) {
                case 0:
                    break;
                case 1:
                    regex += regexSegment.getRegexSegment();
                    break;
                default:
                    break;
            }
        }
        return regex;
    }

    @Override
    public RepetitionStep followedBy() {
        return this;
    }

    @Override
    public BuildStep endRegex() {
        return this;
    }

    @Override
    public FollowedBy once(String content) {
        this.regexSegments.add(new RegexSegment(1, content));
        return this;
    }

    @Override
    public CardinalityStep exactly(int times, String content) {
        this.regexSegments.add(new RegexSegment(times, content));
        return this;
    }

    @Override
    public FollowedBy moreThanOnce(String content) {
        this.regexSegments.add(new RegexSegment(Integer.MAX_VALUE, content));
        return this;
    }

    @Override
    public FollowedBy times() {
        return this;
    }

    @Data
    private class RegexSegment {

        private final int cardinality;

        private final String regexSegment;

    }

}
